package org.springblade.common.utils;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 精度计算工具类
 *
 * @author lpinfo
 */
public class DecimalUtil {


    public static BigDecimal add(BigDecimal value1, BigDecimal value2) {
        BigDecimal result = value1.add(value2);
        return result.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal subtract(BigDecimal value1, BigDecimal value2) {
        BigDecimal result = value1.subtract(value2);
//        System.out.println("=====" + result.setScale(2, BigDecimal.ROUND_HALF_UP));
        return result.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal multiply(BigDecimal value1, BigDecimal value2) {
        BigDecimal result = value1.multiply(value2).setScale(2, BigDecimal.ROUND_HALF_UP);
//        System.out.println("=====result:" + result);
//        System.out.println("=====" + result.setScale(2, BigDecimal.ROUND_HALF_UP));
        return result.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal divide(BigDecimal value1, BigDecimal value2) {
        //TODO 除数不能为0
        BigDecimal result = value1.divide(value2, 2, BigDecimal.ROUND_HALF_UP);
        return result;
    }

    public static void compareBigdecimal(BigDecimal value1, BigDecimal value2) throws RuntimeException {
        int result1 = value1.compareTo(BigDecimal.ZERO);
        int result2 = value2.compareTo(BigDecimal.ZERO);
        if (result1 == -1 || result2 == -1) {
            throw new RuntimeException("金额不能为负数");
        }
    }


    //金额验证
    public static boolean isNumber(String str) {
        // 判断小数点后2位的数字的正则表达式
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");
        Matcher match = pattern.matcher(str);
        if (match.matches() == false) {
            return false;
        } else {
            return true;
        }
    }

    /////FIXME
    public static Boolean defineLessZeroGetTrue(BigDecimal value1) throws RuntimeException {
        int result1 = value1.compareTo(BigDecimal.ZERO);
        if (result1 == -1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * value1 下单金额
     * value2 扣率值
     */
    public static BigDecimal workderRealMoneyRegular(BigDecimal orderPrice, BigDecimal value2) {
        BigDecimal poundage = divide(value2, new BigDecimal("100.00"));
//        System.out.println("===poundage:" + poundage);
        BigDecimal koulv = subtract(new BigDecimal("1.00"), poundage);
//        System.out.println("koulv===" + koulv);
        BigDecimal result = multiply(orderPrice, koulv);
        return result;
    }


    ////////////////////////////////////减法///////////////////////////////////////
//
//
//    public static void main(String[] args) {
////        BigDecimal a = new BigDecimal(137);
////        BigDecimal b = new BigDecimal(12.65);
////        System.out.println("1=" + add(a, b));
////        System.out.println("2=" + subtract(a, b));
////        System.out.println("3=" + multiply(a, b));
////        System.out.println("4=" + divide(a, b));
////        System.out.println("5=" + workderRealMoneyRegular(a, b));
//
//
////        System.out.println("===" + workderRealMoneyRegular(new BigDecimal("31.00"), new BigDecimal("10")));
////
////        BigDecimal value1 = new BigDecimal(0);
////
////        System.out.println(DecimalUtil.securityBigDecimal(value1));
////        System.out.println(DecimalUtil.isNumber(value1.toString()));
////
////        System.out.println(DecimalUtil.defineLessZeroGetTrue(new BigDecimal("-5000.00")));
////
////        if (DecimalUtil.isNumber(value1.toString()) == false) {
////            System.out.println("---1111-");
////        }
//    }

//
//    public static void main(String[] args) {
//        BigDecimal bnum1, bnum2;
//
//        bnum1 = new BigDecimal("10");
//        bnum2 = new BigDecimal("20");
//
//        int res = bnum1.compareTo(bnum2);
//
//        String str1 = "两个数相等";
//        String str2 = "第一个数更大";
//        String str3 = "第二个数更大";
//
//        if (res == 0) {
//            System.out.println(str1);
//        } else if (res == 1) {
//            System.out.println(str2);
//        } else if (res == -1) {
//            System.out.println(str3);
//        }
//    }
}
